{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "71f40897",
   "metadata": {},
   "source": [
    "* 时间：2022-10-31（week11）\n",
    "* 学习记录人：yy\n",
    "* 本周学习主题："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19dc183a",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "* 元组、集合、字典的基本概念和常见方法\n",
    "> 1.元组  \n",
    "> 2.集合  \n",
    "> 3.字典  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06f1e6ac",
   "metadata": {},
   "source": [
    "## 1.元组（tuple）\n",
    "> 1. 概念   \n",
    ">> A.具有和序列数据相同的运算规则：取值（index）和切片（slice）  \n",
    ">> B.表征：（12345，54321，'hello!'），用圆括号/小括号，括起来的，中间仍然是用逗号隔开的数据形态   \n",
    "> 2. 性质：**不可变形**  \n",
    ">> 没有什么什么增删改的方法  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bc9a7b68",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"Head First Python\",88) #创建了一个组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "91355cd6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Head First Python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8e726174",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例：\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ba31e0e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=20, tm_sec=1, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d786d316",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function tuple.count(value, /)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "39d30ae5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(180001, 180002, 18003)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_seq = 180001,180002,18003\n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb323873",
   "metadata": {},
   "source": [
    "## 2.集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c13d89fd",
   "metadata": {},
   "source": [
    "> 1. 集合的概念  \n",
    ">> 1. 没有重复元素的一个数据结构  \n",
    ">> 2. 形态：{'apple','orange','apple','pear','orange','banana'}  \n",
    "> 2. 集合的运算  \n",
    ">> 1. 交集运算:  &  ，intersection  \n",
    ">> 2. 并集运算:  |  ,union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8481185d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'banana', 'orange', 'pear'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef44a540",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1,2,3,4,5,6}\n",
    "合唱团 = {9,2,8,4,10}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "23efa9ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4a99556e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dce967f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 4}\n",
      "{2, 4}\n"
     ]
    }
   ],
   "source": [
    "#交集：\n",
    "print(舞蹈社 & 合唱团)\n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4016f5e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 8, 9, 10}\n",
      "9\n",
      "{1, 2, 3, 4, 5, 6, 8, 9, 10}\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "#并集：统计数量\n",
    "print(舞蹈社 | 合唱团)\n",
    "print(len(舞蹈社 | 合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a60e530",
   "metadata": {},
   "source": [
    "## 3.字典(dict)\n",
    "> 1. 基本的概念  \n",
    ">> A. mapping types:映射类型  \n",
    ">> B. key：values，键：值 对 \n",
    ">> C. 字典的性质  \n",
    ">>> a. 无序性  \n",
    ">>> b. key不可变性  \n",
    ">>> c. key唯一性\n",
    "> 2. 字典的创建  \n",
    ">> \n",
    "> 3.采用的方法  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "20b6443f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "product_list = [\n",
      "    [\"柠檬水\",6],\n",
      "    [\"蜜桃四季春\",10],\n",
      "    [\"芒果奶盖\",12],\n",
      "    [\"葡萄啵啵奶茶\",8]\n",
      "]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"\n",
    "product_list = [\n",
    "    [\"柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "07267d84",
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典的创建\n",
    "product_dict = { \n",
    "    \"柠檬水\":6,\n",
    "    \"蜜桃四季春\":10,\n",
    "    \"芒果奶盖\":12,\n",
    "    \"葡萄啵啵奶茶\":8\n",
    "               }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6e51b356",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'柠檬水': 6, '蜜桃四季春': 10, '芒果奶盖': 12, '葡萄啵啵奶茶': 8} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(product_dict,type(product_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f6bb471b",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [35]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mproduct_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "product_dict[0] #字典是无序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f47e227c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_dict[\"蜜桃四季春\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "14abbe3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info = {\n",
    "    \"hdsdjsdld7856356jhsdjhsd\":[\"abc\",\"shch\",\"shga\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "665fc3ec",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [41]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43muser_info\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhdsdjsdld7856356jhsdjhsd\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "user_info[\"hdsdjsdld7856356jhsdjhsd\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fab32909",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85818552",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ebe9f24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
